package mail.fetch.aliyun;

import mail.util.UnZipper;

import java.io.IOException;
import java.net.CookieManager;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

import static com.han.http.util.Print.printlnInfo;

/**
 * @Author: Hanjiafeng
 * @Date: Created in 17:45 2019/1/23
 * @Desc: 收件箱
 */
@Deprecated
public class Inbox {

    private String getMailList() {
        //设置基本变量
        CookieManager cookieManager = new CookieManager();
        HttpClient client = HttpClient.newBuilder()
                .cookieHandler(cookieManager)
                .build();
        HttpRequest.Builder baseRequestBuilder = HttpRequest.newBuilder()
                .timeout(Duration.ofMinutes(1))
                .setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");

        HttpRequest request = baseRequestBuilder
                .copy()
                .uri(URI.create("https://mail.aliyun.com/alimail/ajax/mail/queryMailList.txt?_timestamp_=1548236003189"))
                .setHeader("Origin", "https://mail.aliyun.com")
                .setHeader("X-Requested-With", "XMLHttpRequest")
                .setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
                .setHeader("DNT", "1")
                .setHeader("Content-Type", "application/x-www-form-urlencoded")
                .setHeader("Accept", "*/*")
                .setHeader("Referer", "https://mail.aliyun.com/alimail/")
                .setHeader("Accept-Encoding", "gzip, deflate, br")
                .setHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .setHeader("Cookie",
                        "alimail_browser_instance=dC0xMDQ2NjQtbllGTzRZ8401;" +
                                "alimail_havana_session_key=QXltU2Vzc2lvbi0xODg0MDYtaWtseEZaWExxRGNSZzVsWXBFNTVlQXFaaW9vQ1hzRkxkZ01Id3dZUzkzZUxtZnZUcW0;" +
                                "havana_session_id=15biEV-dCv03SSfV5FNvpnQ1;" +
                                "alimail_session_version_key=5465780;" +
                                "alimail_auth_session_key=QXltU2Vzc2lvbi0xODg0MDgtbXU2SmhLT2NRQzdmWE1Sd1lWcU5CbHVlSGJ1NVVWZ3cydHNxV2N5c1pMUTlzbVFXdm0;" +
                                "_csrf_token_=QXltVG9rZW4tMjMxMDgwNTUtaDJlZHVqdWNWTTNoNkFMcTRQam5FVXJJU2tGNEJCTVREYUgyOUZHdkJVWUNiYzIzakU;" +
                                "alimail_init_lang=zh_CN;" +
                                "at=\"limindeyou@aliyun.comZLS3w1548231061888\";" +
                                "alimail_session_template_key=v4;" +
                                /*"isg=BDU14T3WhbwX9uEAE9riWq2nRLEvGvmiu4Moj7dak6z7jlSAegAJlqJH3BIdzgF8;" +
                                "UM_distinctid=1687a0e92b63f2-0a77449524069b-b781636-1fa400-1687a0e92b72fa;" +
                                "CNZZDATA1000081634=1624996767-1548233988-https%253A%252F%252Fmail.aliyun.com%252F%7C1548233988;" +*/
                                "havana_heart_beat=1548236001434;" +
                                "udtoken=\"limindeyou@aliyun.com:8834d923d8a2036a1a8b9f664aee046a:7370541548236001730051\"")
                .POST(HttpRequest.BodyPublishers.ofString("showFrom=1&query=%7B%22folderIds%22%3A%5B%222%22%5D%7D&fragment=1&offset=0&length=75&curIncrementId=0&forceReturnData=1&_csrf_token_=QXltVG9rZW4tMjMxMDgwNTUtaDJlZHVqdWNWTTNoNkFMcTRQam5FVXJJU2tGNEJCTVREYUgyOUZHdkJVWUNiYzIzakU&_root_token_=dC0yNjc4NDctT3J5N1hx9861&_refer_hash_=&_tpl_=DEFAULT"))
                .build();

        try {
            HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
            String body = new String(UnZipper.unGzipByBytes(response.body()));
            printlnInfo(body);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }
}
